package cn.lena.idle.util;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageButton;

import java.io.ByteArrayOutputStream;

/**
 * @anthor lena
 * @date 2021/6/11
 */
public class PhotoUtils {

    /**
     * 将ImagaeButton控件的图片转换成二进制
     * @param ivPhoto
     * @return
     */
    public static byte[] ImageButtonToByte(ImageButton ivPhoto){
        //把图片先转化成bitmap格式
        BitmapDrawable drawable = (BitmapDrawable) ivPhoto.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        //二进制数组输出流
        ByteArrayOutputStream byStream = new ByteArrayOutputStream();
        //将图片压缩成质量为100的PNG格式图片
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byStream);
        //把输出流转换为二进制数组
        byte[] byteArray = byStream.toByteArray();
        return byteArray;
    }

}
